package com.ben.multiple.mycat.two.rest;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ben.multiple.mycat.two.entity.PartitionByJumpConsistent;
import com.ben.multiple.mycat.two.service.PartitionByJumpConsistentService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author BenJin Yin
 * @date 2020/7/16
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/partitionByJumpConsistent")
public class PartitionByJumpConsistentResource {
    private final PartitionByJumpConsistentService service;

    @PostMapping("/insert")
    public ResponseEntity<Boolean> insert(@RequestBody PartitionByJumpConsistent model) {
        model.setCreateTime(LocalDateTime.now());
        boolean save = service.save(model);
        return ResponseEntity.ok(save);
    }

    @PostMapping("/insert/batch")
    public ResponseEntity<Boolean> insertBatch(@RequestBody List<PartitionByJumpConsistent> model) {
        model.forEach(obj -> obj.setCreateTime(LocalDateTime.now()));
        int size = model.size();
        long start = System.currentTimeMillis();
        boolean save = service.saveBatch(model);
        long end = System.currentTimeMillis();
        log.info("insertBatch, size:{}, time:{}", size, end - start);
        return ResponseEntity.ok(save);
    }

    @GetMapping("/list")
    public ResponseEntity<List<PartitionByJumpConsistent>> list(@RequestParam(name = "search", required = false) String search) {
        long start = System.currentTimeMillis();
        List<PartitionByJumpConsistent> list = service.list(Wrappers.emptyWrapper());
        long end = System.currentTimeMillis();
        int size = list.size();
        log.info("list, size:{}, time:{}", size, end - start);
        return ResponseEntity.ok(list);
    }

}
